<?php

namespace app\api\controller\applets;

use app\admin\model\MerchantCarTime;
use app\api\model\CarCate;
use app\api\model\MerchantCar;
use app\api\model\Order;
use app\api\model\OrderFace;

/**
 * 定时任务
 */
class Timingtask
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function __construct()
    {
    }

    /**
     *  取车前2个小时 短信提醒商家，还车前两小时 短信提醒商家和用户
     */
    public function orderPickReturnMsg(){
        $time = time() + 2*3600;
        //取车提醒
        $order_list = Order::where(['is_delete' => 1, 'status' => 1, 'pick_merchant_msg' => 1])->where('start_time', '<=', $time)->chunk(20, function ($orders){
            foreach ($orders as $key => $order){
                //向商家发送短信
                $merchant_car = MerchantCar::where(['is_delete' => 1, 'id' => $order['merchant_car_id']])->find();
                $merchant_phone = \app\api\model\Merchant::where(['is_delete' => 1, 'id' => $order['merchant_id']])->value('merchant_phone');
                if($merchant_phone && $merchant_car){
                    $car_cate = CarCate::where('id', $merchant_car['type_id'])->value('cate_name');
                    $data = [
                        'number' => $order['out_trade_no'],
                        'model' => $car_cate,
                        'car_number' => $order['car_number'],
                        'hour' => '2',
                    ];
                    $res = sendMsg($merchant_phone, $data, 'SMS_464071173');
                    if($res['Code'] && $res['Code'] == 'OK'){
                        Order::where('id', $order['id'])->update(['pick_merchant_msg' => 2]);
                    }
                }
            }
        });

        //还车提醒
        $order_list = Order::with('orderContinuePay')->where(['is_delete' => 1, 'status' => 2, 'return_user_msg|return_merchant_msg' => 1])->chunk(20, function ($orders) use ($time) {
            foreach ($orders as $kk => $value){
                if($value['order_continue_pay']){
                    $value['end_time'] = $value['order_continue_pay'][0]['end_time'];
                }
                if($value['end_time'] <= $time){
                    if($value['return_merchant_msg'] == 1){
                        //向商家发送短信
                        $merchant_car = MerchantCar::where(['is_delete' => 1, 'id' => $value['merchant_car_id']])->find();
                        $merchant_phone = \app\api\model\Merchant::where(['is_delete' => 1, 'id' => $value['merchant_id']])->value('merchant_phone');
                        if($merchant_phone && $merchant_car){
                            $car_cate = CarCate::where('id', $merchant_car['type_id'])->value('cate_name');
                            $data = [
                                'number' => $value['out_trade_no'],
                                'model' => $car_cate,
                                'car_number' => $value['car_number'],
                                'hour' => '2',
                            ];
                            $res = sendMsg($merchant_phone, $data, 'SMS_464076258');
                            if($res['Code'] && $res['Code'] == 'OK'){
                                Order::where('id', $value['id'])->update(['return_merchant_msg' => 2]);
                            }
                        }
                    }
                    if($value['return_user_msg'] == 1){
                        //向用户发送短信
                        $merchant_phone = \app\api\model\Merchant::where(['id' => $value['merchant_id']])->value('merchant_phone');
                        $data = [
                            'number' => $value['out_trade_no'],
                            'hour' => '2',
                            'phone' => $merchant_phone
                        ];
                        $res = sendMsg($value['user_phone'], $data, 'SMS_464051267');
                        if($res['Code'] && $res['Code'] == 'OK'){
                            Order::where('id', $value['id'])->update(['return_user_msg' => 2]);
                        }
                    }
                }
            }
        });
    }


    /**
     *  临时下架和上架
     */
    public function tempCarStatus(){
        $car_list = MerchantCarTime::where(['is_delete' => 1])->where('end_time', '>', time())->where('start_time', '<', time())->column('merchant_car_id');
        if($car_list){
            MerchantCar::where(['is_delete' => 1, 'status' => 1])->where('id', 'in', $car_list)->update(['status' => 2, 'off_type' => 2]);
        }else{
            MerchantCar::where(['is_delete' => 1, 'status' => 2, 'off_type' => 2])->update(['status' => 1]);
        }
    }

    /**
     *  商家签约时间到期
     */
    public function merchantSingingTime(){
        $merchant_list = \app\api\model\Merchant::where(['is_delete' => 1, 'examine_status' => 2, 'status' => 1])->where('signing_time', '<', time())->whereNotNull('signing_time')->column('id');
        if($merchant_list){
            \app\api\model\Merchant::where(['is_delete' => 1, 'status' => 1])->where('id', 'in', $merchant_list)->update(['status' => 2]);
        }
    }

    /**
     *  当面付待支付自动取消 20分钟未支付
     */
    public function orderFaceCancel(){
        $last_time = time() - 20*60;
        $order_face_list = OrderFace::field('id,order_id')->where(['is_delete' => 1, 'status' => 1])->where('createtime', '<', $last_time)->select()->toArray();
        if($order_face_list){
            $id_list = array_column($order_face_list, 'id');
            OrderFace::where('id', 'in', $id_list)->update(['status' => 3]);
            $order_id_list = array_column($order_face_list, 'order_id');
            Order::where('id', 'in', $order_id_list)->dec('face_num', 1)->update();
        }
    }

    /**
     *  用户优惠券时间到期
     */
    public function userCouponTime(){
        $list = \app\api\model\UserCoupon::where(['is_delete' => 1, 'status' => 1])->where('expiration_time', '<', time())->column('id');
        if($list){
            \app\api\model\UserCoupon::where('id', 'in', $list)->update(['status' => 3, 'updatetime' => time()]);
        }
    }

}
